package com.cuckoo.mall.thus.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cuckoo.mall.R;


/**
 * 类描述：
 * 创建人：wl
 * 创建时间：2017/4/7 15:07
 */
public class UpdateNumDialog extends Dialog {

    View rootView;
    Context context;
    OnClickOkLister onClickListener;


    public UpdateNumDialog(Context OutContext, String oldValue, OnClickOkLister callback) {
        super(OutContext, R.style.common_diallog_style);
        this.context = OutContext;
        onClickListener = callback;
        /* initView();*/
        rootView = LayoutInflater.from(context).inflate(R.layout.custom_dialog, null, false);

        final EditText editText = rootView.findViewById(R.id.et_content);

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    if (s.toString().equals("0")) {
                        editText.setText("1");
                    } else if (Integer.parseInt(s.toString()) > 200) {
                        editText.setText("200");
                    }
                    //  editText.setSelection(editText.getText().toString().length());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        if (!TextUtils.isEmpty(oldValue)) {
            editText.setText(oldValue);
        }
        TextView addView = rootView.findViewById(R.id.tv_add);
        addView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String textNum = editText.getText().toString();
                if (!TextUtils.isEmpty(textNum) && Integer.parseInt(textNum) < 200) {
                    editText.setText(String.valueOf(Integer.parseInt(textNum) + 1));
                }
            }
        });
        TextView minusView = rootView.findViewById(R.id.tv_minus);
        minusView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String textNum = editText.getText().toString();
                if (!TextUtils.isEmpty(textNum) && Integer.parseInt(textNum) > 1) {
                    editText.setText(String.valueOf(Integer.parseInt(textNum) - 1));
                }
            }
        });

        setContentView(rootView);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
        rootView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rootView.findViewById(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String text = editText.getText().toString();
                if (!TextUtils.isEmpty(text) && !"0".equals(text) && Integer.parseInt(text) <= 200) {
                    onClickListener.onOK(text);
                    dismiss();
                }
            }
        });
    }


    public interface OnClickOkLister {
        void onOK(String data);
    }

}
